﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Data
{
    public static class DataReaderExtensions
    {
        /// <summary>
        /// Gets the value or default.
        /// </summary>
        /// <typeparam name="TReturn">The type of the return.</typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static TReturn GetValueOrDefault<TReturn>(this IDataReader reader, string key, TReturn defaultValue) { 
            if (Convert.IsDBNull(reader[key])) {
                return defaultValue;
            }

            return (TReturn)reader[key];
        }

        /// <summary>
        /// Gets the value or default.
        /// </summary>
        /// <typeparam name="TReturn">The type of the return.</typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="index">The index.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static TReturn GetValueOrDefault<TReturn>(this IDataReader reader, int index, TReturn defaultValue) {
            if (reader.IsDBNull(index)) {
                return defaultValue;
            }

            return (TReturn)reader[index];
        }
    }
}
